<?php

class User_con extends CI_Controller {

    function index() {
        $this->tampil_user();
    }

    function tampil_user() {
        $strquery = "SELECT DISTINCT ID,NAMA FROM user";
        $query = $this->db->query($strquery);
        if ($query->num_rows() > 0) {
            $this->load->library('table');
            $this->table->set_template(array('table_open' => '<table align="center" width="90%" border="1" cellpadding="1" cellspacing="1">'));
            $this->table->set_heading('NO', 'Nama', 'Aksi');
            $i = 1;
            foreach ($query->result() as $entry) {
                $strquery = "SELECT * FROM role natural join daftar_role where ID=" . $entry->ID . " AND (NAMA_ROLE LIKE 'Peserta' OR NAMA_ROLE LIKE 'Pengajar')";
                $query = $this->db->query($strquery);
                if ($query->num_rows() == 0) {
                    $this->table->add_row(
                            $i, anchor(base_url() . 'admin/user_con/view_user/' . $entry->ID, $entry->NAMA), anchor(base_url() . 'admin/user_con/edit_user/' . $entry->ID, 'EDIT')
                            . ' | ' .
                            anchor(base_url() . 'admin/user_con/delete/' . $entry->ID, 'DELETE')
                            . ' | ' .
                            anchor(base_url() . 'admin/user_con/set_role/' . $entry->ID, 'SET ROLE'));
                    $i++;
                }
            }
            $display['content'] = $this->table->generate();
        } else {
            $display['content'] = 'Belum ada data';
        }
        $data['title'] = "List User";
        $this->load->view('admin/admin_header_view', $data);
        $this->load->view('admin/list_user', $display);
        $this->load->view('footer_view');
    }

    function add_user() {
        $data['title'] = "Tambah User";
        $this->load->view('admin/admin_header_view', $data);
        $this->load->view('admin/form_user');
        $this->load->view('footer_view');
    }

    function insert() {
        $_POST['PASSWORD'] = md5($_POST['PASSWORD']);
        $this->db->insert('user', $_POST);
        redirect(base_url() . 'admin/user_con');
    }

    function edit_user($id) {
        $query = $this->db->get_where('user', array('ID' => $id));
        $entry = $query->row();
        $display['content']['ID'] = $entry->ID;
        $display['content']['USERNAME'] = $entry->USERNAME;
        $display['content']['PASSWORD'] = $entry->PASSWORD;
        $display['content']['NAMA'] = $entry->NAMA;
        $display['content']['EMAIL'] = $entry->EMAIL;
        $display['content']['ALAMAT'] = $entry->ALAMAT;
        $display['content']['NO_TELP'] = $entry->NO_TELP;

        $data['title'] = "Edit User";
        $this->load->view('admin/admin_header_view', $data);
        $this->load->view('admin/edit_user', $display);
        $this->load->view('footer_view');
    }

    function update() {
        $_POST['PASSWORD'] = md5($_POST['PASSWORD']);
        $this->db->where('ID', $_POST['ID']);
        $this->db->update('user', $_POST);
        redirect(base_url() . '/admin/user_con');
    }

    function delete($id) {
        $tables = array('user', 'role');
        $this->db->where('ID', $id);
        $this->db->delete($tables);
        redirect(base_url() . '/admin/user_con');
    }

    function set_role($id) {
        $query = $this->db->get_where('user', array('ID' => $id));
        $display['id'] = $query->row()->ID;
        $display['nama'] = $query->row()->NAMA;
        $str_query = "SELECT *
            FROM daftar_role
            WHERE ID_ROLE NOT
            IN (
            SELECT ID_ROLE
            FROM role
            WHERE ID =" . $id . "
            )";
        $query = $this->db->query($str_query);
        $i = 0;
        foreach ($query->result() as $entry) {
            $display['cekbok'][$i]['id'] = $entry->ID_ROLE;
            $display['cekbok'][$i]['nama'] = $entry->NAMA_ROLE;
            $i++;
        }
        $data['title'] = "Set Role";
        $this->load->view('admin/admin_header_view', $data);
        $this->load->view('admin/role_user', $display);
        $this->load->view('footer_view');
    }

    function input_role() {
        $pilih = $this->input->post('role');
        $id = $this->input->post('id');
        for ($i = 0; $i < count($pilih); $i++) {
            if (isset($pilih[$i])) {
                $this->db->insert('role', array('ID' => $id, 'ID_ROLE' => $pilih[$i]));
            }
        }
        redirect(base_url() . 'admin/user_con');
    }

    function view_user($id) {
        $query = $this->db->get_where('user', array('ID' => $id));
        $row = $query->row();
        $display['id'] = $row->ID;
        $display['nama'] = $row->NAMA;
        $display['username'] = $row->USERNAME;
        $display['email'] = $row->EMAIL;
        $display['alamat'] = $row->ALAMAT;
        $display['telp'] = $row->NO_TELP;
        $join = 'SELECT * FROM `role` NATURAL JOIN `daftar_role` WHERE ID =' . $id;
        $query = $this->db->query($join);
        if ($query->num_rows() > 0) {
            $i = 0;
            foreach ($query->result() as $entry) {
                $display['role'][$i]['idrole'] = $entry->ID_ROLE;
                $display['role'][$i]['namarole'] = $entry->NAMA_ROLE;
                $i++;
            }
        } else {
            $display['role'] = 'Tidak ada role';
        }
        $data['title'] = "Data User";
        $this->load->view('admin/admin_header_view', $data);
        $this->load->view('admin/data_user', $display);
        $this->load->view('footer_view');
    }

    function delete_role() {
        $id = $this->input->post('id');
        $this->db->delete('role', array('ID' => $id));
        $pilih = $this->input->post('role');
        $id = $this->input->post('id');
        for ($i = 0; $i < count($pilih); $i++) {
            $this->db->insert('role', array('ID' => $id, 'ID_ROLE' => $pilih[$i]));
        }
        redirect(base_url() . 'admin/user_con');
    }
    

}

?>
